﻿using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using NotifyPilot.Models;
using Yammer.api;

namespace NotifyPilot.WCF.Helpers
{
    public class YammerConfigParametersFactory
    {
        public static YammerConfigParameters LoadParameters()
        {
            try
            {
                return Helpers.DeserialiseXmlFromFile<YammerConfigParameters>(ConfigPath.YammerConfigFile);
            }
            catch (Exception)
            {
                return new YammerConfigParameters();
            }
        }

        public static YammerClient CreateYammerClient(YammerConfigParameters config)
        {
            return new YammerClient(config.ConsumerKey, config.ConsumerSecret, config.BackUrl, config.Code, config.Token);
        }
    }

    public partial class YammerConfigParameters
    {
        public String ConsumerKey { get; set; }
        public String ConsumerSecret { get; set; }
        public String Code { get; set; }
        public String Token { get; set; }
        public long PublishGroup { get; set; }
        public String DefaultTopic { get; set; }
        public String BackUrl { get; set; }

        public Boolean HaveRequiredParameters
        {
            get
            {
                return !String.IsNullOrEmpty(this.ConsumerKey) &&
                       !String.IsNullOrEmpty(this.ConsumerSecret) &&
                       this.PublishGroup > 0;
            }
        }

        public Boolean IsProballyReadyToPost
        {
            get
            {
                return (HaveRequiredParameters ||
                       !String.IsNullOrEmpty(this.Token));
            }
        }

        public Boolean Save()
        {
            return Helpers.SerializeObjectToFile(this, ConfigPath.YammerConfigFile);
        }
    }

    /// <summary>
    /// Wrapper of User Parameters around Yammer subject
    /// </summary>
    public partial class YammerUserParameters
    {
        public long YammerId { get; set; }
        public String Code { get; set; }
        public String Token { get; set; }

        public override string ToString()
        {
            using (var ms = new MemoryStream())
            {
                var serializer = new JsonSerializer();
                // serialize product to BSON
                var writer = new BsonWriter(ms);
                serializer.Serialize(writer, this);
                return Convert.ToBase64String(ms.ToArray());
            }
        }

        public static implicit operator YammerUserParameters(String bson)
        {
            if (bson != null)
            {
                try
                {
                    using (var ms = new MemoryStream(Convert.FromBase64String(bson)))
                    {
                        var serializer = new JsonSerializer();
                        var reader = new BsonReader(ms);

                        var deserializedParams = serializer.Deserialize<YammerUserParameters>(reader);
                        return deserializedParams;
                    }
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return null;
        }
    }
}